<?php
	function Model($table_name = null){
		return new Model($table_name);
	}


	class MysqlConnection{

		static function get_connection(){
			$config = include('config.php');
			extract($config);
			$connection = mysql_connect($db_host,$db_user,$db_pass);
			mysql_select_db($db_name,$connection);
			mysql_query("set names utf8",$connection);
			return $connection;
		}
	}


	class Model{
		var $table_name;
		var $condition;

		public function __construct($table_name = null){
			if($table_name){
				$this->table($table_name);
			}
		}

		public function table($table_name){
			$config = include('config.php');
			$this->table_name = $config['db_prefix'] . $table_name;
			return $this;
		}

		public function where($condition){
			$this->condition = $condition;
			return $this;
		}

		/**
		 * 暂时不需要
		 * @return [type] [description]
		 */
		public function select(){
			// $sql =
		}

		public function insert($data,$is_replace = false){
			$head = ($is_replace ? "replace into `{$this->table_name}`" : "insert into `{$this->table_name}`");
			$keys = array();
			$values = array();
			foreach ($data as $key => $val) {
				$keys[] = "`$key`";
				if(is_null($val)){
					$values[] = "null";
				} else {
					$values[] = "'$val'";
				}
			}


			$sql = $head . '(' . implode(',',$keys) . ')values(' . implode(',',$values) . ')';
			$connection = MysqlConnection::get_connection();
			$ret = mysql_query($sql,$connection);
			file_put_contents("sqllog",$sql);
			if($ret){
				return mysql_insert_id();
			}
			return false;
		}

		public function delete(){
			$head = "delete from `{$this->table_name}` ";
			$sql = $head . $this->get_where();
			$ret = mysql_query($sql,MysqlConnection::get_connection());
			return mysql_affected_rows();
		}

		private function get_where(){
			if($this->condition){
				$head = 'where ';
				$buffer = array();
				foreach($this->condition as $key => $val){
					$buffer[] = "$key = '$val'";
				}
				return $head . implode(' and ',$buffer);
			}
			return '';
		}

		public function update($data){
			$head = "update `{$this->table_name}` set ";
			$buffer = array();
			foreach($data as $key => $val){
				$buffer[] = "$key = '$val'";
			}
			$sql = $head . implode(',',$buffer) . $this->get_where();
			$ret = mysql_query($sql,MysqlConnection::get_connection());
			return mysql_affected_rows();
		}

		public function query($sql){
			$arr = array();
			$ret = mysql_query($sql,MysqlConnection::get_connection());
			if($ret){
				while($row = mysql_fetch_assoc($ret)){
					$arr[] = $row;
				}
			}
			return $arr;
		}

		public function find($main_id = null){
			$sql = "select * from " . $this->table_name . ' ' . $this->get_where() . ' limit 1';
			$ret = mysql_query($sql,MysqlConnection::get_connection());
			if($ret){
				return mysql_fetch_assoc($ret);
			}
			return null;
		}

		public function count(){
			$sql = "select count(*) as num from " . $this->table_name . ' ' . $this->get_where();
			$ret = mysql_query($sql,MysqlConnection::get_connection());
			if($ret){
				$row = mysql_fetch_assoc($ret);
				return $row['num'];
			}
			return 0;
		}

	}
?>